using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public enum ProjectileBehavior
{
	Explosion,
	Nullify,
	Link,
    Static
}

public class Character : MonoBehaviour 
{
	#region Static Member
	
	public static Character Instance;

    #endregion

    public FirstPersonView m_View;
	public FPSController m_FpsController;			
	
	public List<GameObject> m_linked;				// Liste des objets lié par la gravité 
	public float m_ValGravity;						// Valeur reliant les objets selectionnées
	
	public GameObject m_ObjectGrabed=null;			// Objet que le joueur porte

	public ProjectileBehavior m_ModeProjectile;			// Mode du projectile			
	public Projectile projectileFired;				// Référence vers le projectile tiré
	public GameObject refExplosion;					// Référence vers l'explosion
	
	float m_MaxMagnitude = 20.0f;	
	float m_Acceleration=1.0f;
	
	float coef=200.0f;

	// Use this for initialization

    void Awake()
    {
        Instance = this;
        m_View = transform.FindChild("Camera").GetComponent<FirstPersonView>();
        Debug.Log(m_View);
    }

	void Start () 
	{
        m_ObjectGrabed = null;
		
		m_ValGravity=10.0f;
		Instance=this;
		
		m_ModeProjectile=ProjectileBehavior.Explosion;
        Gui.Instance.m_ProjectileBehavior = "Explosion";

	}
	
	// Update is called once per frame
	void Update () 
	{
		Screen.lockCursor=true;
		
		m_ValGravity+=(Input.GetAxis("Mouse ScrollWheel")*1000.0f);
	
		if(Input.GetMouseButtonDown(1))
		{
			SwitchWeapon();
		}
		if(Input.GetMouseButtonDown(0) && !projectileFired)
		{
			if(refExplosion!=null)
			{
				if(refExplosion.GetComponent<PExplosion>().pushButton)
				{
					refExplosion.GetComponent<PExplosion>().button.SwitchReleased();	
				}
				Destroy(refExplosion);	
			}
			Quaternion now= transform.rotation;

             GameObject projectile = (GameObject)GameObject.Instantiate(Resources.Load("Prefabs/p_Projectile"),
									m_View.transform.position,
									Quaternion.identity);
			
			
			projectile.rigidbody.velocity=this.transform.rigidbody.velocity.magnitude * m_View.transform.forward;
			
			projectile.transform.GetComponent<ConstantForce>().force= (m_View.transform.forward*coef);

		}
		
		if(Input.GetKeyDown(KeyCode.E))
		{
			if(m_ObjectGrabed==null)
			{
				GameObject [] g = GameObject.FindGameObjectsWithTag("Cube");

				for(int i=0;i<g.Length;i++)
				{
					g[i].rigidbody.WakeUp();	
				}
               
				
				GameObject.FindGameObjectWithTag("Char").rigidbody.WakeUp();
			
				RaycastHit objectHitted= new RaycastHit();

				int layer =0;
				layer= 1 <<11;


                if (Physics.Raycast(new Ray(m_View.transform.position, m_View.transform.forward), out objectHitted, 100.0f, layer))
				{
					if(objectHitted.collider.tag=="Cube")
					{
						if(objectHitted.distance<4.0f)
						{
							m_ObjectGrabed=objectHitted.collider.gameObject;
							m_ObjectGrabed.rigidbody.useGravity=false;
						}
					}
                }
                
			}
            else
            {
                if (!m_ObjectGrabed.GetComponent<Cube>().noGravity)
                {
                    m_ObjectGrabed.transform.rigidbody.useGravity = true;
                  
                }
                m_ObjectGrabed = null;
               
            }
			
        }

        if (Input.GetKeyDown(KeyCode.G))
        {
            if (m_ObjectGrabed != null)
            {
                m_ObjectGrabed.rigidbody.velocity=( m_View.transform.forward * 10.0f);

                if (!m_ObjectGrabed.GetComponent<Cube>().noGravity)
                {
                    m_ObjectGrabed.transform.rigidbody.useGravity = true;

                }
                m_ObjectGrabed = null;
              
            }
        }
	}
	
	public void SwitchWeapon()
	{
		
		m_ModeProjectile=(ProjectileBehavior) (((int)(m_ModeProjectile)+1)%4);
		
		switch (m_ModeProjectile)
		{
			case ProjectileBehavior.Explosion :
			    Gui.Instance.m_ProjectileBehavior="Explosion";
			break;
			case ProjectileBehavior.Nullify :
			Gui.Instance.m_ProjectileBehavior="Nullify";
			break;
				case ProjectileBehavior.Link :
			Gui.Instance.m_ProjectileBehavior="Link";
			break;
                case ProjectileBehavior.Static:
            Gui.Instance.m_ProjectileBehavior = "Static";
            break;
		}
	}
}
